<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        .grid {
            margin: auto;
            width: 500px;
            text-align: center;
        }

        .grid table {
            width: 100%;
            border-collapse: collapse;
        }

        .grid th td {
            padding: 100;
            border: 1p dashed orange;
            height: 35px;
            line-height: 35px;
        }

        .grid th {
            background-color: orange;
        }

        .grid .book {
            width: 500px;
            background-color: orange;
        }

        .grid .total {
            width: 100%;
            height: 35px;
            background-color: orange;
            line-height: 35px;
            border: 1px solid rgb(161, 148, 148);
            box-sizing: border-box;
        }
    </style>
</head>

<body>
    <div id="example">
        <div class="grid">
            <div>
                <h1>图书管理</h1>
                <div class="book">
                    <div>
                        <label for="id">编号</label>
                        <input type="text" id="id" v-model="id" :disabled="flag"></input>
                        <label for="name">名称</label>
                        <input type="text" id="name" v-model="name"></input>
                        <button @click="handle" :disabled="subFlag">提交</button>
                    </div>
                </div>
                <div class="total">
                    <span>图片总数:</span>
                    <span>{{total}}</span>
                </div>
            </div>
            <table>
                <thead>
                    <tr>
                        <th>编号</th>
                        <th>名称</th>
                        <th>时间</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    <tr v-for="(item,index) in books" :key="index">
                        <td>{{item.id}}</td>
                        <td>{{item.name}}</td>
                        <td>{{item.dat | format('yyyy-MM-dd')}}</td>
                        <td>
                            <!-- 阻止默认行为 ：@click.preevent。后面可以不用跟着方法名-->
                            <a href="javascript:;" @click.preevent="toEdit(item.id)">修改</a>
                            <span>|</span>
                            <a href="javascript:;" @click.preevent="toDele(item.id)">删除</a>
                        </td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>

    <script src="../../js/vue.js"></script>
    <script>

        var vm = new Vue({
            el: '#example',
            data: {
                // books: [{
                //     id: 1,
                //     name: "三国演义",
                //     dat: new Date()
                // }, {
                //     id: 2,
                //     name: "红楼梦",
                //     dat: new Date()
                // }, {
                //     id: 3,
                //     name: "水浒传",
                //     dat: new Date()
                // }, {
                //     id: 4,
                //     name: "西游记",
                //     dat: new Date()
                // }],
                books:[],
                id: '',
                name: '',
                flag: false,
                subFlag: false
            },
            filters: {
                "format": function (value, arg) {
                    if (arg == "yyyy-MM-dd") {
                        ret = value.getFullYear() + '-' + (value.getMonth() + 1) + '-' + value.getDate();
                        return ret;
                    }
                    return value
                }
            },
            methods: {
                handle() {
                    if (this.flag) {
                        this.books.some((item) => {
                            if (item.id == this.id) {
                                item.name = this.name
                                return true
                            }
                        })
                    } else {
                        var book = {}
                        book.id = this.id
                        book.name = this.name
                        book.dat = new Date()
                        this.books.push(book)
                    }
                    this.id = ''
                    this.name = ''
                    this.flag = false
                },
                toEdit(id) {
                    // console.log('id', id)
                    var book = this.books.filter(function (item) {
                        return item.id == id;
                    })
                    // console.log('book', book)
                    this.id = book[0].id
                    this.name = book[0].name
                    this.flag = true
                },
                toDele(id) {
                    this.books = this.books.filter(function (item) {
                        return item.id != id
                    })
                    // --------------
                    // var index = this.books.findIndex(function(item){
                    //     return item.id == id
                    // })
                    // this.books.splice(index, 1)
                }
            },
            computed: {
                total: function () {
                    console.log('length', this.books.length)
                    return this.books.length;
                }
            },
            watch: {
                name(val) {
                    var flag = this.books.some((item) => {
                        return item.name == val
                    })
                    if (flag) {
                        this.subFlag = true
                    } else {
                        this.subFlag = false
                    }
                }
            },
            mounted() { // 生命周期
                // 该生命周期钩子函数被触发的时候，模板已经可以使用了。
                // 一般此时用于获取后台数据，然后把数据填充到模板
                var data = [{
                    id: 1,
                    name: "三国演义",
                    dat: new Date()
                }, {
                    id: 2,
                    name: "红楼梦",
                    dat: new Date()
                }, {
                    id: 3,
                    name: "水浒传",
                    dat: new Date()
                }, {
                    id: 4,
                    name: "西游记",
                    dat: new Date()
                }]
                this.books = data
            }
        })
    </script>
</body>

</html>